package  com.globagames.strategicgames.business
{
	import com.globagames.strategicgames.model.Session;
	import com.globagames.strategicgames.model.*;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.data.mxml.DataService;
	import mx.rpc.IResponder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;



	public class SessionDelegate
	{
		private var service:DataService;
		
		public function SessionDelegate( responder : IResponder )
		{		
			this.responder = responder;
			service = new DataService();
			service.destination = "sessionService";
			service.autoCommit = true;		
		}
		public function doCreateItem( session:Session) : void
		{										
			var call : Object = this.service.createItem(session);
			this.service.commit();
			call.addResponder( responder );			
		}
		
		public function doCommit() : void
		{					
			trace("SessionDelegate doCommit");								
			this.service.commit();						
		}
		public function doDisconnect() : void
		{					
			this.service.disconnect();				
		}
		
		public function doFill() : void
		{			
			//var players:ArrayCollection = StrategicGameModelLocator.getInstance().players;			
			this.service.fill(StrategicGameModelLocator.getInstance().players);
		}
		public function doRemove(session:Session) : void
		{					
			var call : Object = this.service.deleteItem(session);
			this.service.commit();
			
			call.addResponder( responder );					
		}
		private function resultHandler(event:ResultEvent):void
		{
			this.responder.result(event);
		}
		
		private function faultHandler(event:FaultEvent):void
		{
			Alert.show(event.fault.rootCause.message, event.fault.faultCode);
			this.responder.fault(event);
		}
									
		private var responder : IResponder;

	}

}